package com.yanqu.road.dao.impl.activity.goddess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.goddess.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoddessUserDataDaoImpl extends TempDataObjectDao<GoddessUserData> {


    public static String getGoddessUserDataSql = "select * from `t_u_activity_goddess_user_data` where `activityId`=? and `userId`=?";
    public GoddessUserData getGoddessUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGoddessUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGoddessUserDataSql = "update `t_u_activity_goddess_user_data` set energy=?,lastEnergyRecTime=?,score=?,cellPoint=?,passMissNum=?,cellList=?,colorStoneMap=?,yinStoneList=?,yangStoneList=?,humanNum=?,era=?," +
            "makeYear=?,yearStatus=?,historyMap=?,peoMap=?,rankCellList=?,rankColorStoneMap=?,rankYinStoneList=?,rankYangStoneList=?,rankEra=?,rankCellPoint=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(GoddessUserData goddessUserData) {
        goddessUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getLastEnergyRecTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getCellPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getPassMissNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getCellList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getColorStoneMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getYinStoneList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getYangStoneList())));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getHumanNum()));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getEra()));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getMakeYear()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getYearStatus())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getHistoryMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getPeoMap())));

        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getRankCellList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getRankColorStoneMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getRankYinStoneList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getRankYangStoneList())));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getRankEra()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getRankCellPoint()));

        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getUserId()));
        if (getDb().executeNoneQuery(updateGoddessUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGoddessUserDataSql = "insert into `t_u_activity_goddess_user_data`(activityId,userId,energy,lastEnergyRecTime,score,cellPoint,passMissNum,cellList,colorStoneMap,yinStoneList,yangStoneList,humanNum,era," +
            "makeYear,yearStatus,historyMap,peoMap,rankCellList,rankColorStoneMap,rankYinStoneList,rankYangStoneList,rankEra,rankCellPoint) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(GoddessUserData goddessUserData) {
        goddessUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getLastEnergyRecTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getCellPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getPassMissNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getCellList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getColorStoneMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getYinStoneList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getYangStoneList())));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getHumanNum()));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getEra()));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getMakeYear()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getYearStatus())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getHistoryMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getPeoMap())));

        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getRankCellList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getRankColorStoneMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getRankYinStoneList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goddessUserData.getRankYangStoneList())));
        para.put(i++, new DbParameter(Types.INTEGER, goddessUserData.getRankEra()));
        para.put(i++, new DbParameter(Types.BIGINT, goddessUserData.getRankCellPoint()));

        if (getDb().executeNoneQuery(insertGoddessUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GoddessUserData parseData(ResultSet rs) throws SQLException {
        GoddessUserData goddessUserData = new GoddessUserData();
        goddessUserData.setActivityId(rs.getInt("activityId"));
        goddessUserData.setUserId(rs.getLong("userId"));
        goddessUserData.setEnergy(rs.getInt("energy"));
        goddessUserData.setLastEnergyRecTime(rs.getLong("lastEnergyRecTime"));
        goddessUserData.setScore(rs.getLong("score"));
        goddessUserData.setCellPoint(rs.getLong("cellPoint"));
        goddessUserData.setPassMissNum(rs.getInt("passMissNum"));
        goddessUserData.setCellList(JSON.parseObject(rs.getString("cellList"), new TypeReference<List<GoddessCell>>() {}));
        goddessUserData.setColorStoneMap(JSON.parseObject(rs.getString("colorStoneMap"), new TypeReference<Map<Integer, GoddessColorStone>>() {}));
        goddessUserData.setYinStoneList(JSON.parseObject(rs.getString("yinStoneList"), new TypeReference<List<GoddessYinYangStone>>() {}));
        goddessUserData.setYangStoneList(JSON.parseObject(rs.getString("yangStoneList"), new TypeReference<List<GoddessYinYangStone>>() {}));
        goddessUserData.setHumanNum(rs.getLong("humanNum"));
        goddessUserData.setEra(rs.getInt("era"));
        goddessUserData.setMakeYear(rs.getInt("makeYear"));
        goddessUserData.setYearStatus(JSON.parseObject(rs.getString("yearStatus"), GoddessYearStatus.class));
        goddessUserData.setHistoryMap(JSON.parseObject(rs.getString("historyMap"), new TypeReference<Map<Integer, List<GoddessEraHistory>>>(){}));
        goddessUserData.setPeoMap(JSON.parseObject(rs.getString("peoMap"), new TypeReference<Map<Integer, Integer>>(){}));


        String rankCellList = rs.getString("rankCellList");
        if(!StringUtils.isNullOrEmpty(rankCellList)){
            goddessUserData.setRankCellList(JSON.parseObject(rankCellList, new TypeReference<List<GoddessCell>>() {}));
        }

        String rankColorStoneMap = rs.getString("rankColorStoneMap");
        if(!StringUtils.isNullOrEmpty(rankColorStoneMap)){
            goddessUserData.setRankColorStoneMap(JSON.parseObject(rankColorStoneMap, new TypeReference<Map<Integer, GoddessColorStone>>() {}));
        }

        String rankYinStoneList = rs.getString("rankYinStoneList");
        if(!StringUtils.isNullOrEmpty(rankYinStoneList)){
            goddessUserData.setRankYinStoneList(JSON.parseObject(rankYinStoneList, new TypeReference<List<GoddessYinYangStone>>() {}));
        }

        String rankYangStoneList = rs.getString("rankYangStoneList");
        if(!StringUtils.isNullOrEmpty(rankYangStoneList)){
            goddessUserData.setRankYangStoneList(JSON.parseObject(rankYangStoneList, new TypeReference<List<GoddessYinYangStone>>() {}));
        }
        goddessUserData.setRankEra(rs.getInt("rankEra"));
        goddessUserData.setRankCellPoint(rs.getLong("rankCellPoint"));


        return goddessUserData;
    }
}
